﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;
using PSDLib.Ctrl;

namespace PSDManager.SystemSettings
{
    public partial class StoragePanel : UserControl
    {
        private int storageType = 0;
        private DiskInfo selectedDisk;
        private List<dl_download_device_storage> storages;
        public StoragePanel()
        {
            InitializeComponent();
        }

        public void SetStorageType(int type)
        {
            storageType = type;

            if (storageType == 0)
            {
                lblBackup2.Visible = true;
                pbBackup2.Visible = true;
                lblBackup1.Visible = true;
                pbBackup1.Visible = true;
                lblMain2.Visible = true;
                pbMain2.Visible = true;
            }
            else
            {
                if (storageType == 2)
                {
                    pbMain2.Visible = false;
                    lblMain2.Visible = false;
                }
                else
                {
                    pbMain2.Visible = true;
                    lblMain2.Visible = true;
                }

                pbBackup1.Visible = false;
                lblBackup1.Visible = false;
                pbBackup2.Visible = false;
                lblBackup2.Visible = false;
            }
        }
        public void SetStorages(List<dl_download_device_storage> storageList)
        {
            storages = storageList;

            foreach (var storage in storageList)
            {
                DiskInfo disk = new DiskInfo();
                disk.Name = storage.id;
                disk.DiskDriver = storage.storage_driver;
                disk.VolumeName = storage.storage_name;
                disk.SumSpace = storage.storage_total_size;
                disk.FreeSpace = storage.storage_free_size;
                disk.StorageType = storage.storage_type;
                disk.Group = storage.storage_group;
                disk.Downloading = (storage.state == 1);
                disk.Dock = DockStyle.Top;

                disk.DiskClick += Disk_DiskClick;
                panel1.Controls.Add(disk);
                disk.BringToFront();
            }
        }

        private void Disk_DiskClick(object sender, EventArgs e)
        {
            selectedDisk = (sender as DiskInfo);

            foreach (DiskInfo disk in panel1.Controls)
            {
                if (disk.Name == selectedDisk.Name)
                {
                    disk.BackColor = Color.LightSteelBlue;
                }
                else
                {
                    disk.BackColor = Color.White;
                }
            }
        }

        private void btnMain1_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                var m = storages.Where(t => t.storage_type == 0 && t.storage_group == 1 && t.storage_driver != selectedDisk.DiskDriver).FirstOrDefault();

                if (m != null)
                {
                    MessageBox.Show("已设置 " + m.storage_driver + " 为主存-1，请重新选择！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                selectedDisk.StorageType = 0;
                selectedDisk.Group = 1;

                var storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();

                storage.storage_type = 0;
                storage.storage_group = 1;
            }
        }

        private void btnMain2_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                var m = storages.Where(t => t.storage_type == 0 && t.storage_group == 2 && t.storage_driver != selectedDisk.DiskDriver).FirstOrDefault();

                if (m != null)
                {
                    MessageBox.Show("已设置 " + m.storage_driver + " 为主存-2，请重新选择！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                selectedDisk.StorageType = 0;
                selectedDisk.Group = 2;

                var storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();

                storage.storage_type = 0;
                storage.storage_group = 2;
            }
        }

        private void btnBackup1_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                var m = storages.Where(t => t.storage_type == 1 && t.storage_group == 1 && t.storage_driver != selectedDisk.DiskDriver).FirstOrDefault();

                if (m != null)
                {
                    MessageBox.Show("已设置 " + m.storage_driver + " 为备存-1，请重新选择！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                selectedDisk.StorageType = 1;
                selectedDisk.Group = 1;

                var storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();

                storage.storage_type = 1;
                storage.storage_group = 1;
            }
        }

        private void btnBackup2_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                var m = storages.Where(t => t.storage_type == 1 && t.storage_group == 2 && t.storage_driver != selectedDisk.DiskDriver).FirstOrDefault();

                if (m != null)
                {
                    MessageBox.Show("已设置 " + m.storage_driver + " 为备存-2，请重新选择！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                selectedDisk.StorageType = 1;
                selectedDisk.Group = 2;

                var storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();

                storage.storage_type = 1;
                storage.storage_group = 2;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                selectedDisk.StorageType = -1;
                selectedDisk.Group = 0;
                selectedDisk.Downloading = false;

                var storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();

                storage.storage_type = -1;
                storage.storage_group = 0;
                storage.state = 0;
            }
        }

        private void pbActive_Click(object sender, EventArgs e)
        {
            if (selectedDisk != null)
            {
                if (selectedDisk.StorageType == 0)
                {
                    dl_download_device_storage storage = null;

                    foreach (DiskInfo disk in panel1.Controls)
                    {
                        disk.Downloading = false;
                        storage = storages.Where(t => t.id == disk.Name).FirstOrDefault();
                        storage.state = 0;
                    }
                    selectedDisk.Downloading = true;

                    storage = storages.Where(t => t.id == selectedDisk.Name).FirstOrDefault();
                    storage.state = 1;
                }
                else
                {
                    MessageBox.Show("只有主存储才可被设置为工作盘");
                }
            }
        }

        public void ResetStorage(dl_download_device device)
        {
            if (device != null)
            {
                if (device.storage_type == 0)
                {
                    if (storages.Count < 2)
                    {
                        MessageBox.Show("请放入至少两块硬盘！");
                    }
                    var list = storages.OrderBy(t => t.storage_driver).ToList();

                    for (int i = 0; i < list.Count; i++)
                    {
                        selectedDisk = (DiskInfo)panel1.Controls.Find(list[i].id, false)[0];

                        if (i == 0)
                        {
                            btnMain1_Click(null, null);
                            pbActive_Click(null, null);
                        }
                        else if (i == 1)
                        {
                            btnBackup1_Click(null, null);
                        }
                        else if (i == 2)
                        {
                            btnMain2_Click(null, null);
                        }
                        else if (i == 3)
                        {
                            btnBackup2_Click(null, null);
                        }
                    }
                }
                else if (device.storage_type == 1)
                {
                    if (storages.Count < 2)
                    {
                        MessageBox.Show("请放入至少两块硬盘！");
                    }
                    var list = storages.OrderBy(t => t.storage_driver).ToList();

                    for (int i = 0; i < list.Count; i++)
                    {
                        selectedDisk = (DiskInfo)panel1.Controls.Find(list[i].id, false)[0];

                        if (i == 0)
                        {
                            btnMain1_Click(null, null);
                            pbActive_Click(null, null);
                        }
                        else if (i == 1)
                        {
                            btnBackup1_Click(null, null);
                        }
                    }
                }
                else if (device.storage_type == 2)
                {
                    if (storages.Count < 1)
                    {
                        MessageBox.Show("请放入至少一块硬盘！");
                    }
                    var list = storages.OrderBy(t => t.storage_driver).ToList();

                    selectedDisk = (DiskInfo)panel1.Controls.Find(list[0].id, false)[0];

                    btnMain1_Click(null, null);
                    pbActive_Click(null, null);
                }
            }
        }
    }
}
